package com.zx.mes.poi.service;

import com.zx.mes.poi.entity.IsEntity;
import com.zx.mes.poi.utils.ExcelUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TestService {
    /**
     * 导出excel
     * @param
     */
    public HSSFWorkbook downloadExcel() {
        try {
            //查询数据
            List<IsEntity > query = new ArrayList<>();
            IsEntity isEntity1 = new IsEntity();
            isEntity1.setId("1");
            isEntity1.setName("xxx");
            isEntity1.setPassWord("yy");
            IsEntity isEntity2 = new IsEntity();
            isEntity2.setId("2");
            isEntity2.setName("11111");
            isEntity2.setPassWord("cccc");
            query.add(isEntity1);
            query.add(isEntity2);
            //标题
            Map<String, String> column = new TreeMap<>();
            column.put("A1","excel标题1");
            column.put("A2","excel标题2");
            column.put("A3","excel标题3");

            //excel内容
            List<Map<String, Object>> listResult = new ArrayList<Map<String, Object>>();
            if(query!=null && !query.isEmpty()){

                for (IsEntity vo: query) {
                    Map<String, Object> hashMap = new HashMap<String, Object>();
                    hashMap.put("A1",vo.getId());
                    hashMap.put("A2",vo.getName());
                    hashMap.put("A3",vo.getPassWord());

                    listResult.add(hashMap);
                }
            }

            //去调用工具类的方法
            HSSFWorkbook wb = ExcelUtils.getHSSFWorkbook(listResult, column);
            return wb;
        } catch (Exception e) {
            //TODO处理异常
        }
        return null;
    }
}
